package com.ruoyi.project.module.category.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.module.category.domain.Category;
import com.ruoyi.project.module.category.service.ICategoryService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 商品类目 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-08-06
 */
@Controller
@RequestMapping("/module/category")
public class CategoryController extends BaseController
{
    private String prefix = "module/category";
	
	@Autowired
	private ICategoryService categoryService;
	
	@RequiresPermissions("module:category:view")
	@GetMapping()
	public String category()
	{
	    return prefix + "/category";
	}
	
	/**
	 * 查询商品类目列表
	 */
	@RequiresPermissions("module:category:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Category category)
	{
		startPage();
        List<Category> list = categoryService.selectCategoryList(category);
		return getDataTable(list);
	}
	
	/**
	 * 新增商品类目
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存商品类目
	 */
	@RequiresPermissions("module:category:add")
	@Log(title = "商品类目", action = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Category category)
	{		
		return toAjax(categoryService.insertCategory(category));
	}

	/**
	 * 修改商品类目
	 */
	@GetMapping("/edit/{categoryId}")
	public String edit(@PathVariable("categoryId") Integer categoryId, ModelMap mmap)
	{
		Category category = categoryService.selectCategoryById(categoryId);
		mmap.put("category", category);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存商品类目
	 */
	@RequiresPermissions("module:category:edit")
	@Log(title = "商品类目", action = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Category category)
	{		
		return toAjax(categoryService.updateCategory(category));
	}
	
	/**
	 * 删除商品类目
	 */
	@RequiresPermissions("module:category:remove")
	@Log(title = "商品类目", action = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(categoryService.deleteCategoryByIds(ids));
	}
	
}
